package com.xiaoyu.client.ui.fragment.main;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.xiaoyu.client.ClientConstants;
import com.xiaoyu.client.R;
import com.xiaoyu.client.model.mine.PersonalBean;
import com.xiaoyu.client.network.NetworkManager;
import com.xiaoyu.client.ui.activity.main.mine.collect_and_refundafter.CollectActivity;
import com.xiaoyu.client.ui.activity.main.mine.my_info.FansActivity;
import com.xiaoyu.client.ui.activity.main.mine.my_info.FocusoncountActivity;
import com.xiaoyu.client.ui.activity.main.mine.my_order.MyOrderActivity;
import com.xiaoyu.client.ui.activity.main.mine.set_favorable.FavorableActivity;
import com.xiaoyu.client.ui.activity.main.mine.my_info.ModifiedDataActivity;
import com.xiaoyu.client.ui.activity.main.mine.wallet_and_member.MyMemberActivity;
import com.xiaoyu.client.ui.activity.main.mine.set_favorable.SetActivity;
import com.xiaoyu.client.ui.activity.main.mine.wallet_and_member.WalletActivity;
import com.xiaoyu.client.ui.activity.main.mine.sign_and_help.SignActivity;
import com.xiaoyu.commonlib.network.CommonHttpResponser;
import com.xiaoyu.commonlib.utils.CCircleImageView;
import com.xiaoyu.commonlib.utils.StatusBarUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 我的
 * Created by jk on 2018/9/28.
 */
public class MyFragment extends Fragment {
    public static final String USER_ID = "my_info_userid";
    public static final String MY_INTEGRAL = "my_integral";
    private View mView;
    @BindView(R.id.nickname)
    TextView nickName;
    @BindView(R.id.userphoto)
    CCircleImageView mUserPhoto;
    @BindView(R.id.individuation)
    TextView individuation;
    @BindView(R.id.focusoncount)
    TextView focusoncount;
    @BindView(R.id.fanscount)
    TextView fanscount;
    @BindView(R.id.isVip)
    ImageView mIsVip;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        if (mView == null) {
            mView = inflater.inflate(R.layout.fragment_my, container, false);
            //绑定fragment
            ButterKnife.bind(this, mView);
        }
        ViewGroup parent = (ViewGroup) mView.getParent();
        if (parent != null) {
            parent.removeView(mView);
        }
        return mView;
    }

    @Override
    public void onResume() {
        super.onResume();
        getInfo();
    }

    //接口获取个人信息
    private void getInfo() {
        NetworkManager.getPersonalInfo(new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                succeedResultInfo(result);
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {

            }
        });
    }

    //解析数据
    private void succeedResultInfo(String result) {
        PersonalBean bean = new Gson().fromJson(result, PersonalBean.class);
        nickName.setText(bean.getData().getNickname().equals("") ? "追忆似水年华" : bean.getData().getNickname());//用户昵称
        Glide.with(getActivity()).load(bean.getData().getUserphoto()).into(mUserPhoto);//头像

        individuation.setText(bean.getData().getIndividuation().equals("") ? "输入你的个性签名" : bean.getData().getIndividuation());//个性签名
        focusoncount.setText(bean.getData().getFocusoncount().equals("") ? "0关注" : bean.getData().getFocusoncount() + "关注");//关注个数
        fanscount.setText(bean.getData().getFanscount().equals("") ? "0粉丝" : bean.getData().getFanscount() + "粉丝");//粉丝数量
        if (bean.getData().getVip().equals("1")) { // "1"==是Vip  "0"==不是Vip
            mIsVip.setVisibility(View.VISIBLE);
        } else {
            mIsVip.setVisibility(View.GONE);
        }
    }

    //修改昵称
    @OnClick(R.id.userphoto)
    public void modifiedDataClick() {
        startActivity(new Intent(getActivity(), ModifiedDataActivity.class));
    }

    //签到
    @OnClick(R.id.fragment_my_sign)
    public void signClick() {
        startActivity(new Intent(getActivity(), SignActivity.class));
    }

    //设置
    @OnClick(R.id.my_relative_set)
    public void setClick() {
        startActivity(new Intent(getActivity(), SetActivity.class));
    }

    //我的钱包
    @OnClick(R.id.fragment_my_wallet)
    public void walletClick() {
        startActivity(new Intent(getActivity(), WalletActivity.class));
    }

    //我的会员
    @OnClick(R.id.fragment_my_member)
    public void memberClick() {
        startActivity(new Intent(getActivity(), MyMemberActivity.class));
    }

    //我的优惠卷
    @OnClick(R.id.fragment_my_favorable)
    public void favorableClick() {
        startActivity(new Intent(getActivity(), FavorableActivity.class));
    }

    //我的积分
    @OnClick(R.id.fragment_my_integral)
    public void myIntegralClick() {
        Intent intent = new Intent(getActivity(), SignActivity.class);
        intent.putExtra(MY_INTEGRAL, "my_integral");
        startActivity(intent);
    }

    //我的收藏
    @OnClick(R.id.mine_collect)
    public void mineCollectClick() {
        startActivity(new Intent(getActivity(), CollectActivity.class));
    }

    //我的订单
    @OnClick(R.id.mine_order)
    public void mineOrderClick() {
        startActivity(new Intent(getActivity(), MyOrderActivity.class));
    }

    //我的购物车
//    @OnClick(R.id.fragment_my_shopping_cart)
//    public void mineShopCartClick() {
//    }

    //关注
    @OnClick(R.id.focusoncount)
    public void focusoncountClick() {

        Intent intent = new Intent(getActivity(), FocusoncountActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt(ClientConstants.FROM, ClientConstants.FROM_MY);
        bundle.putString(USER_ID, "");
        intent.putExtras(bundle);
        startActivity(intent);
    }

    //粉丝
    @OnClick(R.id.fanscount)
    public void fanscountClick() {
        Intent intent = new Intent(getActivity(), FansActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt(ClientConstants.FROM, ClientConstants.FROM_MY);
        bundle.putString(USER_ID, "");
        intent.putExtras(bundle);
        startActivity(intent);
    }

}
